package cn.amaake.magicplugin.rpc.autoconfigure;

import cn.amaake.magicplugin.rpc.config.DubboConfig;
import cn.amaake.magicplugin.rpc.controller.MagicRpcController;
//import cn.amaake.magicplugin.rpc.modules.RpcProviderModule;
import cn.amaake.magicplugin.rpc.modules.RpcProviderModule;
import cn.amaake.magicplugin.rpc.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.ssssssss.magicapi.core.config.MagicAPIProperties;
import org.ssssssss.magicapi.core.config.MagicPluginConfiguration;
import org.ssssssss.magicapi.core.model.Plugin;
import org.ssssssss.magicapi.core.service.MagicResourceService;
import org.ssssssss.magicapi.core.web.MagicControllerRegister;
//import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource;
//import org.ssssssss.magicapi.datasource.service.DataSourceInfoMagicResourceStorage;
//import org.ssssssss.magicapi.datasource.service.DataSourceMagicDynamicRegistry;

@Configuration
@EnableConfigurationProperties(DubboConfig.class)
public class MagicAPIRpcConfiguration implements MagicPluginConfiguration {

    private final Logger logger = LoggerFactory.getLogger(MagicAPIRpcConfiguration.class);
    private final MagicAPIProperties properties;
    private final DubboConfig dubboConfig;
    private final ApplicationContext applicationContext;

    public MagicAPIRpcConfiguration(MagicAPIProperties properties, DubboConfig dubboConfig, ApplicationContext applicationContext) {
        this.properties = properties;
        this.dubboConfig = dubboConfig;
        this.applicationContext = applicationContext;
    }

    @Override
    public Plugin plugin() {
        return new Plugin("RPC远程调用", "MagicRpc", "magic-rpc.1.0.0.iife.js");
    }

    @Override
    public MagicControllerRegister controllerRegister() {
        return (mapping, configuration) -> mapping.registerController(new MagicRpcController(configuration,applicationContext));
    }

    @Bean
    @ConditionalOnMissingBean
    public RpcInfoMagicResourceStorage rpcInfoMagicResourceStorage() {
        return new RpcInfoMagicResourceStorage();
    }

    /**
     * Dobbo服务
     */
    @Bean
    @ConditionalOnMissingBean
    public DobboService dobboService( MagicResourceService magicResourceService) {
        return new DobboService(applicationContext,dubboConfig,magicResourceService);
    }

    @Bean
    @ConditionalOnMissingBean
    public RpcMagicDynamicRegistry rpcMagicDynamicRegistry(RpcInfoMagicResourceStorage rpcInfoMagicResourceStorage,DobboService dobboService) {
        return new RpcMagicDynamicRegistry(rpcInfoMagicResourceStorage,dobboService);
    }

//    @Bean
//    @ConditionalOnMissingBean
//    public RpcProviderInfoMagicResourceStorage rpcProviderInfoMagicResourceStorage() {
//        return new RpcProviderInfoMagicResourceStorage();
//    }
//
//    @Bean
//    @ConditionalOnMissingBean
//    public RpcProviderInfoMagicDynamicRegistry rpcProviderInfoMagicDynamicRegistry(RpcProviderInfoMagicResourceStorage rpcProviderInfoMagicResourceStorage) {
//        return new RpcProviderInfoMagicDynamicRegistry(rpcProviderInfoMagicResourceStorage);
//    }
//
    @Bean
    @ConditionalOnMissingBean
    public RpcProviderModule rpcProviderModule() {
        return new RpcProviderModule(applicationContext);
    }

}
